(function() {
    angular.module('app').controller('industryCtrl', industryCtrl);

    industryCtrl.$inject = ['$scope', 'util', '$rootScope','user'];

    function industryCtrl($scope, util, $rootScope,user) {
        console.log('---------------------------------');


        

        // console.log('industryCtrl controller js');
        var vm = this;

        vm.user = user;

        console.log('----------------industryCtrl--------------');
        console.log(user);

        init();

        function init() {

            var itemLevel1Category = vm.user.itemLevel1Category;

           

            // console.log(itemLevel1Category);

            var version = vm.user.version;


    

            // console.log(version);


            if (version.indexOf('v1') != -1) { //全行业

                vm.catName = '/';

            } else if (version.indexOf('v2') != -1) { //多行业

                if (itemLevel1Category == null || itemLevel1Category.length == 0) {
                    vm.catName = "/";
                } else {
                    var arry = itemLevel1Category.split(';');

                    if ($rootScope.userInfo.industry_selectIndex) {

                        console.log('已有默认类目');

                        vm.catName = arry[$rootScope.userInfo.industry_selectIndex];

                    } else {
                        vm.catName = arry[0];
                    }


                }


            } else { //单行业

                // 行业未选择
                if (itemLevel1Category == null || itemLevel1Category.length == 0) {

                    vm.catName = "/";

                    // 组件industry.select.component
                    $rootScope.$emit('industry_select_EV');

                } else {
                    vm.catName = itemLevel1Category;
                }
            }

        }

        
        var clearFun = $rootScope.$on('industry-catName-change',function(e,catName){

            vm.catName = catName;
        });

        $scope.$on('$destroy',function(){
            clearFun();
        });



        vm.config = [{
            icon: 'fa fa-line-chart teal-text', //icon
            title: '月销售额', //title
            date: '', //date
            body: 'sellMoney', //字段
            footer: 'sellMoneyGrowth'
        }, {
            icon: 'fa fa-bar-chart red-txet', //icon
            title: '月销售量', //title
            date: '', //date
            body: 'sellAmount',
            footer: 'sellAmountGrowth'
        }, {
            icon: 'fa fa-home green-text', //icon
            title: '店铺数', //title
            date: '', //date
            body: 'shopNum'
        }, {
            icon: 'fa fa-shopping-bag red-text', //icon
            title: '宝贝数', //title
            date: '', //date
            body: 'productNum'
        }];


        vm.configline = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function(params) {
                    // console.log(params);
                    var str = '<div style = "width:120px;text-align: left;">' + params[0].name + '</div>';

                    for (var i = 0; i < params.length; i++) {
                        str += '<div style = "text-align: left;padding-left:10px;"><span style="width:10px;height:10px;display:inline-block;border-radius:50%;background:' + params[i].color + '"></span>  ' + params[i].seriesName + ' : ' + util.bigNumFormat(params[i].data) + '</div>';
                    }

                    return str;
                }
            },
            grid: {
                left: 40,
                // right: 100,
                // bottom: 10
            },
            legend: {
                data: ['总和', '天猫', '淘宝', '天猫国际', '全球购'],
                x: 25,
                y: 15
            },
            xAxis: {
                data: [],
                axisLabel: {
                    //rotate: 50,
                    show: true
                }
            },
            yAxis: [{
                type: 'value',
                axisLabel: {
                    formatter: function(value, index) {
                        var len = String(value).length;

                        if (value < 10000) {
                            return value;
                        }

                        switch (len) {
                            case 5:
                                return (value / 10000) + '万';
                            case 6:
                                return (value / 10000) + '万';
                            case 7:
                                return (value / 10000) + '万';
                            case 8:
                                return (value / 10000) + '万';
                            case 9:
                                return (value / 100000000) + '亿';
                            case 10:
                                return (value / 100000000) + '亿';
                            case 11:
                                return (value / 100000000) + '亿';
                            case 12:
                                return (value / 100000000) + '亿';
                        }

                        return value;
                    }
                }
            }],
            series: [{
                name: '总和',
                type: 'line',
                data: []
            }, {
                name: '天猫',
                type: 'line',
                data: []
            }, {
                name: '淘宝',
                type: 'line',
                data: [],
                label: {
                    normal: {
                        formatter: function(value) {
                            return '元';
                        }
                    },
                    emphasis: function(value) {
                        return '33';
                    }
                }
            }, {
                name: '天猫国际',
                type: 'line',
                data: []
            }, {
                name: '全球购',
                type: 'line',
                data: []
            }]
        };

        vm.tabSeen = false; //是否展示tab

        // $scope.$watch('vm.catName', function(newValue, oldValue) {
        //     console.log('controller catName watch:' + newValue);

        // });


    }
})();
